package com.heykui.restfulcrud.controller;

import javax.servlet.http.HttpServletRequest;

import com.heykui.restfulcrud.model.User;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReqParam {

    @GetMapping("/user")
    public String get(String name, Integer id) {
        return "name= " + name + ", id= " + id;
    }

    @GetMapping("/user/{uid}")
    public String get2(@PathVariable Integer uid){
        return "uid = " + uid;
    }

    @RequestMapping("/userinfo")
    public String get3(HttpServletRequest request){
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        return "name= " + name + " age = " + age; 
    }

    // TODO: 失败
    @RequestMapping("/adduser")
    public String adduser(User user) {
        return user.toString();
    }
    
    // TODO: 失败
    @PostMapping("/adduser1")
    public String adduser1(@ModelAttribute("user") User user) {
        return user.toString();
    }

    @GetMapping("/user1")
    public String user1(@RequestParam("name") String username) {
        return "username = " + username;
    }

    // TODO: 失败
    @PostMapping("/user2")
    public String user2(@RequestBody User user) {
        return user.toString();
    }
    
}